package com.egg.ylt.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.activity.ACT_ConfirmPayment;
import com.egg.ylt.activity.ACT_MealCardDetails;
import com.egg.ylt.activity.ACT_UserLogin;
import com.egg.ylt.pojo.CardDataEntity;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ViewHolder;
import com.yonyou.framework.library.common.utils.StringUtil;

import java.math.BigDecimal;

/**
 * Created by lijiateng on 2019/1/16.
 */

public class ADA_TimesCard extends CommonAdapter<CardDataEntity.CardListEntity> {

    public ADA_TimesCard(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_times_card;
    }

    @Override
    protected void convert(ViewHolder holder, final CardDataEntity.CardListEntity cardListEntity, int position) {

        String str = cardListEntity.getName();
        float scale = 0;

        int length = str.length();

        if (length >= 10 && length < 12) {
            scale = 0.9f;
        } else if (length >= 12 && length < 14) {
            scale = 0.8f;
        } else if (length >= 14 && length < 16) {
            scale = 0.7f;
        } else {
            scale = 0.8f;
        }

//        if (length > 10) {
//            switch (length) {
//                case 11:
//                    scale = 0.8f;
//                    break;
//                case 12:
//                    scale = 0.7f;
//                    break;
//                case 13:
//                    scale = 0.6f;
//                    break;
//                case 14:
//                    scale = 0.5f;
//                    break;
//
//            }
//        }
        holder.setSpanText(R.id.tv_card_name, StringSpanUtils.init().addRelativeScale(scale,cardListEntity.getName()).build());

        holder.setText(R.id.tv_card_curr_price,
                StringUtil.getFormattedMoney(StringUtil.getString(cardListEntity.getPrice())) + "");

        TextView originPrice = holder.getView(R.id.tv_card_origin_price);
        originPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.setText(R.id.tv_card_origin_price,
                "原价：" + StringUtil.getFormattedMoney(StringUtil.getString(cardListEntity.getTotalPrice())) + " 元");

        holder.setText(R.id.tv_card_name_below, cardListEntity.getName());
        holder.setText(R.id.tv_card_spec, cardListEntity.getEndDate());

        // 跳转次卡详情页
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ACT_MealCardDetails.class);
                intent.putExtra("cardId", cardListEntity.getId());
                intent.putExtra("shopId", cardListEntity.getShopId());
                intent.putExtra("shopName", cardListEntity.getShopName());
                intent.putExtra("cardImageUrl", cardListEntity.getImageUrl());
                mContext.startActivity(intent);
            }
        });

        // 跳转购买次卡
        holder.setOnClickListener(R.id.tv_buy_card, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 未登录跳转登录页面
                if (TextUtils.isEmpty(Constants.TOKEN)) {
                    Intent intent = new Intent(mContext, ACT_UserLogin.class);
                    mContext.startActivity(intent);
                    return;
                }

                Intent intent = new Intent(mContext, ACT_ConfirmPayment.class);
                Bundle bundle = new Bundle();
                bundle.putString("cardImageUrl", cardListEntity.getImageUrl());
                bundle.putSerializable("cardId", cardListEntity.getId());
                bundle.putString("cardName", cardListEntity.getName());
                bundle.putString("price", cardListEntity.getPrice());
                bundle.putString("endDate", cardListEntity.getEndDate());
                bundle.putString("applyType", "2");
                bundle.putString("shopName", cardListEntity.getShopName());
                bundle.putString("shopId", cardListEntity.getShopId());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }
}
